package com.ushine.web.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ushine.web.base.mapper.RoleMapper;
import com.ushine.web.base.model.system.RoleDO;
import com.ushine.web.base.service.IRoleService;
import com.ushine.web.component.page.PageRequestQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: yls
 * @Date: 2021/3/15 9:23
 * @Description: 角色管理 -- serviceImpl
 * @Version 1.0
 */
@Service
@Transactional(rollbackFor = {Exception.class})
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleDO> implements IRoleService {

    @Override
    @Transactional(readOnly = true)
    public List<RoleDO> listAllRoles() {
        return list();
    }


    @Override
    @Transactional(readOnly = true)
    public RoleDO getRoleById(String roleId) {
        return getById(roleId);
    }

    @Override
    @Transactional(readOnly = true)
    public RoleDO getRoleByName(String roleName) {
        LambdaQueryWrapper<RoleDO> queryWrapper = new LambdaQueryWrapper<>();
        List<RoleDO> roles = list(queryWrapper.eq(RoleDO::getName, roleName));
        return roles.isEmpty() ? null : roles.get(0);
    }

    @Override
    public Boolean insertRole(RoleDO roleDO) {
        return saveOrUpdate(roleDO);
    }

    @Override
    public Boolean deleteRoles(List<String> roleIds) {
        return removeByIds(roleIds);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<RoleDO> listRolesByPage(PageRequestQuery pageRequestQuery) {
        Page<RoleDO> page = new Page<>(pageRequestQuery.getPageNum(), pageRequestQuery.getPageSize());
        QueryWrapper<RoleDO> queryWrapper = new QueryWrapper<>();
        return page(page, queryWrapper);
    }

}
